﻿@page "/designer"
@using System.Diagnostics.CodeAnalysis
@using WinFormBlazorDesigner.Components
@using WinformBlazor.Components
<div class="container-fluid designer" style="height:100vh">
    <div class="row" style="height:5%">
        <div class="col">
            <Menu IsAccordion="true" Items="@Items" DisableNavigation="true" OnClick="MenuOnClick" />
        </div>
    </div>
    <div class="row" style="height:5%">
        <div class="col">
        </div>
    </div>
    <div class="row" style="height: calc(90% - 2px);margin-top: 2px;">
        <div class="col-2" style="@ShowLeft">
            <Tab Placement="Placement.Left">
                <TabItem Text="工具箱" Icon="fa-solid fa-user">
                    <Button>Update</Button>
                    @foreach (ContrloType item in Enum.GetValues(typeof(ContrloType)))
                    {
                        <div draggable="true"
                             @ondragstart="(e)=>{Page.SelectedBox=item;Page.SelectedBoxOffx=e.OffsetX;Page.SelectedBoxOffy=e.OffsetY;}"
                             @ondragend="()=>{Page.SelectedBox=null;}">
                            @item.ToString();
                        </div>
                    }

                </TabItem>
                <TabItem Text="工具箱2">
                    asd
                </TabItem>
                <TabItem Text="工具箱3">
                    asd
                </TabItem>
            </Tab>
        </div>
        <CascadingValue Value="Page">
            <div class="col">

                <DControl Data="@Page.MainPage"></DControl>

            </div>
            <div class="col-2 right" style="@ShowRight">
                <Tab>
                    <TabItem Text="样式">
                        @if (Page.SelectControl != null)
                        {
                            <PropertiesPanel ControlInfo="Page.SelectControl"></PropertiesPanel>
                        }
                    </TabItem>
                    <TabItem Text="数据源">
                        数据源
                    </TabItem>
                </Tab>

            </div>
        </CascadingValue>
    </div>
</div>



@code {
    public string ShowLeft { get; set; } = "";
    public string ShowRight { get; set; } = "";
    private IEnumerable<MenuItem>? Items { get; set; }
    [Parameter, NotNull]
    public Page? Page { get; set; } = new();
    protected override async Task OnInitializedAsync()
    {
        await base.OnInitializedAsync();
        Page.UpdateHander = Update;
        Items = new List<MenuItem>()
        {
        new MenuItem("文件"){  Items=new List<MenuItem>()
        {
             new MenuItem("新建"),
             new MenuItem("打开"),
        } },
        new MenuItem("编辑"){  Items=new List<MenuItem>()
        {
             new MenuItem("转到"),
             new MenuItem("查找和替换"),
        } },
        new MenuItem("视图"){ Items=new   List<MenuItem>()
{
             new MenuItem("工具箱"){ Icon="fas fa-check"  },
             new MenuItem("属性栏"){ Icon="fas fa-check"  },
             new MenuItem("Debug窗口"){ Id="sdf"},
        }
        },

        };

    }
    protected override async Task OnParametersSetAsync()
    {
        await base.OnParametersSetAsync();
    }
    /// <summary>
    /// todo 优化为更新中间和右边,因为左边工具箱不会变化
    /// </summary>
    public void Update()
    {
        this.StateHasChanged();
    }
    Task MenuOnClick(MenuItem item)
    {
        if (item.Text == "工具箱")
        {

            ShowLeft = ShowLeft == "" ? "display:none;" : "";
            item.Icon = ShowLeft == "" ? "fas fa-check" : "";
        }
        else if (item.Text == "属性栏")
        {

            ShowRight = ShowRight == "" ? "display:none;" : "";
            item.Icon = ShowRight == "" ? "fas fa-check" : "";
        }
        StateHasChanged();
        return Task.CompletedTask;
    }

}
